#include "creature.h"

#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include "creature.h"

using namespace std;

Creature::Creature() {}

Creature::Creature(int name)
{
	m_id = name;
}

Creature::Creature(int name, int x, int y, Flag flag) {}

Creature::~Creature() {}
/*判断生理状况并确定行动模式*/
int Creature::situation()
{
	int temp;
	if (m_hp <= 0)
	{
		if (m_hunger <= 0)
		{
			m_dead = 3;
		}
		else
		{
			m_dead = 2;
		}
		record();
		return m_live = 2;
	} /*重伤则死,生成记录并退出函数*/
	if (nature.time_past % 10 == 0)
	{
		m_age++;
	} /*年龄增长，每10time年龄加一*/
	if (m_age >= age_up)
	{
		m_dead = 1;
		record();
		return m_live = 2;
	}
	/*到寿则死，生成记录并退出函数*/
	if (m_hunger <= 20)
	{
		temp = 20 - m_hunger;
		m_hunger = 0;
		m_hp -= temp;
	} /*当饥饿值不够时扣除血量*/
	else
	{
		m_hunger -= 20;
	} /*正常消耗,一次20*/
	if (m_hp <= hp_up)
	{
		temp = hp_up - m_hp;
		if (temp <= 20)
		{
			m_hunger -= temp;
			m_hp += temp;
		}
		else
		{
			m_hunger -= 20;
			m_hp += 20;
		}
	}					/*如果受伤则额外消耗能量疗伤*/
	if (m_hunger >= 80) /*满血的同时还能支持4回合不吃不喝行动则进入繁衍*/
	{
		return m_mode = 2;
	}
	return m_mode = 1; /*正常情况下直接设定行动模式为觅食*/
}

/*动作，生物一个回合内所作的全部动作都在这里*/
void Creature::action()
{
	situation(); /*计算生理情况，并确定搜索对象*/
	if (m_live == 2)
	{
		return;
	}						 /*如果死亡则退出函数*/
	search(m_mode);			 /*搜索，得出目标*/
	move(*m_target, m_mode); /*由目标和模式做出行动*/
	/*完成后将变量归位*/
	m_target = nullptr; /*完成后重定义target结构体将其复位*/
	m_direction = {0, 0};
	m_distance = 9999;
}
/*搜索，遍历整个生物数组*/
void Creature::search(int mode)
{
	int temp = 2;
	Creature *c;
	temp -= mode;							  /*temp-mode后的值刚好觅食为1繁衍为0，直接和race对比即可实现目标选择*/
	for (int i = 1; i <= nature.num.all; i++) /*全遍历*/
	{
		c = nature.livings[i];
		if (c->m_position.x <= m_position.x + m_sight || c->m_position.x >= m_position.x - m_sight) /*搜索对象在范围内*/
		{
			if (c->m_position.y <= m_position.y + m_sight || c->m_position.y >= m_position.y - m_sight)
			{
				if (nature.livings[i]->m_live == 1) /*对象活着*/
				{
					if (nature.livings[i]->m_race < m_race) /*对方种族低于自己，则是猎食者，需要躲避*/
					{
						get_distance(*c); /*测距并将其视为目标*/
						m_mode = 3;		  /*设置模式为逃离*/
						break;			  /*退出搜索循环*/
					}
					else if (nature.livings[i]->m_race == temp) /*不是猎食者的情况下，则看是不是目标对象*/
					{
						get_distance(*c); /*测距并将其视为目标*/
					}
				}
			}
		}
	}
	if (m_target == nullptr) /*如果搜索一圈后没有结果，则行动模式改为闲逛*/
	{
		m_mode = 0;
	}
}

void Creature::move(Creature &c, int mode)
{
	if (mode == 3)
	{
	}
}

/*向某个对象移动，还需要一个变量来确定移动的数值是正还是负，要理一下逻辑*/
void Creature::move_to(Creature &c)
{
	if (m_distance <= m_step) /*距离小于移动距离则直接移动过去*/
	{
		m_position.x = c.m_position.x;
		m_position.y = c.m_position.y;
		m_target.contact = 1; /*接触到了目标*/
	}
	else /*如果距离不够则计算k并向该方向移动*/
	{
		m_position.x += m_step * m_target.distance_x;
		m_position.y += m_step * m_target.distance_y * m_target.k;
		m_target.contact = 0; /*由于没有接触到目标，所以将contact改为3,eat和breed要先判断contact的值*/
	}
}
/*随机移动，需要确定怎么生成随机值*/
void Creature::move_to()
{
	int temp_x = rand() % 20 - 10;
	int temp_y = rand() % 20 - 10;
	int temp_k = 1; /*方向*/
	if (temp_x != 0)
	{
		int temp_k = temp_y / temp_x + 1;
	}
	m_position.x += m_step * (rand() % 2 - 1);
	m_position.y += m_step * (rand() % 2 - 1) * temp_k;
	m_target.contact = 0;
}

void Creature::move_out(Creature &c) /*远离，所以说是-=*/
{
	int temp_mov = m_step;
	m_position.x -= temp_mov * m_target.distance_x;
	m_position.y -= temp_mov * m_target.distance_y * m_target.k;
}

/*进食*/
void Creature::eat(Creature &c)
{
	if (m_target.contact == 1)
	{
		m_hunger += c.m_hp;
		c.m_hp = 0;
		c.m_flag.dead = 2;	/*对象生物先改标签和血量，到它的Situation再清理*/
		m_flag.can_eat = 2; /*死掉的对象不能吃*/
		record(c);
	}
}
/*繁衍*/
void Creature::breed()
{
	if (m_target.contact == 1 && m_target.self->m_flag.can_breed == 1) /*接触且同时对方可生殖则进行繁衍*/
	{
		m_hunger -= 40;
		m_hp -= 40; /*繁衍消耗体力，则Hp和Hunger都扣*/
		if (m_flag.gender == 2)
		{
			*m_target.num += 1; /*总数往后拨一位*/
			switch (this->m_flag.race)
			{
			case 1:
				m_target.race[*m_target.num] = new Wolf(*m_target.num, m_position.x, m_position.y, m_target.self->m_flag);
				m_flag.can_breed = 0;
				m_target.self->m_flag.can_breed = 0; /*狼不能连续生殖，将两狼都调为0*/
				break;
			case 2:
				m_target.race[*m_target.num] = new Sheep(*m_target.num, m_position.x, m_position.y, m_target.self->m_flag);
				break;
			default:
				break;
			}
			record(*this, *m_target.num);
		}
	}
}

void Creature::get_distance(Creature &c) /*计算距离，如果距离小于最短距离则更换对象*/
{
	int temp_x, temp_y, temp_distance;
	temp_x = c.m_position.x - m_position.x;
	temp_y = c.m_position.y - m_position.y;
	temp_distance = temp_x * temp_x + temp_y * temp_y;
	if (temp_distance <= m_distance) /*如果对象距离最小*/
	{
		m_target = &c; /*对象*/
		m_distance = temp_distance;
		m_direction.x = temp_x;
		m_direction.y = temp_y; /*确定位移量*/
	}
}
/*记录死亡事件*/
void Creature::record()
{
	nature.events[nature.temp_events] = new Events(nature.time_past, m_position, m_name, m_flag.race);
	nature.events[nature.temp_events]->get(m_flag.dead);
	nature.temp_events++;
	nature.num.events++;
}
/*记录捕食事件*/
void Creature::record(Creature &c)
{
}
/*记录繁衍时间*/
void Creature::record(Creature &c, int name)
{
	nature.events[nature.temp_events] = new Events(nature.time_past, m_position, m_name, m_flag.race);
	nature.events[nature.temp_events]->get(c, name);
	nature.temp_events++;
	nature.num.events++;
}
